package com.dizner.androiduploadlibrary;

import android.app.ActivityManager;
import android.app.Application;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.support.annotation.LayoutRes;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.dizner.androiduploadlibrary.javabean.ApplicationUpdateInfoBean;
import com.dizner.androiduploadlibrary.service.UpGradeService;

import java.lang.ref.SoftReference;
import java.util.List;

import static android.content.Context.MODE_PRIVATE;

public final class UpTestGradeManager {
    private static Builder builder;
    private static boolean showed;

    private UpTestGradeManager() {
    }

    public static Builder getUpgrodeBuilder(Context context) {
        builder = new Builder(context);

        return builder;
    }


    public void showUpgradeInfo() {
        Intent intent = new Intent(builder.mContext.get(), UpGradeService.class);
        intent.putExtra("actionType", "upgrade");
        intent.putExtra("apkStorePath", builder.apkStorePath);
        intent.putExtra("updateInfoBean", builder.updateInfoBean);
        intent.putExtra("isAuto", UpTestGradeManager.Builder.isAuto);
        if (builder.versionCode < builder.updateInfoBean.getNewVersionCode()) {
            if (!showed || !UpTestGradeManager.Builder.isAuto || builder.updateInfoBean.isMandatory()) {
                if (!isServiceRunning(builder.mContext.get(), UpGradeService.class.getName())) {
                    builder.mContext.get().startService(intent);
                    showed = true;
                } else {
                    Toast.makeText(builder.mContext.get(), "正在更新", Toast.LENGTH_SHORT).show();
                    Log.i("UpTestGradeManager", "服务已启动");
                }
            }
        }
    }

    public void showUpgradeInfo(@LayoutRes int dialogViewResId) {
        Intent intent = new Intent(builder.mContext.get(), UpGradeService.class);
        intent.putExtra("actionType", "upgrade");
        intent.putExtra("dialogViewResId", dialogViewResId);
        intent.putExtra("apkStorePath", builder.apkStorePath);
        intent.putExtra("updateInfoBean", builder.updateInfoBean);
        if (builder.versionCode < builder.updateInfoBean.getNewVersionCode()) {
            if (builder.versionCode < builder.updateInfoBean.getNewVersionCode()) {
                if (!isServiceRunning(builder.mContext.get(), UpGradeService.class.getName())) {
                    builder.mContext.get().startService(intent);
                    showed = true;
                } else {
                    Toast.makeText(builder.mContext.get(), "正在更新", Toast.LENGTH_SHORT).show();
                    Log.i("UpTestGradeManager", "服务已启动");
                }
            }
        }

    }


    public void InstallApk(String apkPath) {
        Intent intent = new Intent(builder.mContext.get(), InstallApplicationTestActivity.class);
        intent.putExtra("apkPath", apkPath);
        builder.mContext.get().startActivity(intent);
    }


    public static class Builder {

        private SoftReference<Context> mContext;
        private String apkStorePath;
        private ApplicationUpdateInfoBean updateInfoBean;
        private int versionCode;
        private SharedPreferences sp;

        public static boolean isAuto;
        public static View contentView;

        private Builder(Context mContext) {
            this.mContext = new SoftReference<>(mContext);
            isAuto = false;
            contentView = null;
            sp = mContext.getSharedPreferences(UpGradeService.SP_NAME, MODE_PRIVATE);
            try {
                versionCode = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0).versionCode;
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }

        public UpTestGradeManager build() {
            return new UpTestGradeManager();
        }

        public Builder setApkStorePath(String apkStorePath) {
            this.apkStorePath = apkStorePath;
            return this;
        }


        public Builder setApplicationUpdateInfoBean(ApplicationUpdateInfoBean updateInfoBean) {
            this.updateInfoBean = updateInfoBean;
            return this;
        }

        public Builder setIsAuto(boolean isAuto) {
            Builder.isAuto = isAuto;
            return this;
        }

        public View getContentView() {
            return Builder.contentView;
        }


    }

    /**
     * 判断服务是否运行
     */
    private boolean isServiceRunning(Context context, final String className) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> info = activityManager.getRunningServices(Integer.MAX_VALUE);
        if (info == null || info.size() == 0) return false;
        for (ActivityManager.RunningServiceInfo aInfo : info) {
            if (className.equals(aInfo.service.getClassName())) return true;
        }
        return false;
    }


}
